<?php

namespace App\Http\Controllers\Web;

use App\ConstDir\ErrorConst;
use App\Repository\Repositories\Interfaces\OrderProductCommentRepository;
use App\Repository\Repositories\Interfaces\ProductSkusRepository;
use App\Repository\Repositories\Interfaces\ProductsRepository;
use Illuminate\Http\Request;

/**
 * 订单管理控制器
 *
 * @package App\Http\Controllers\Backend
 */
class ProductSkuController extends BaseController
{
    /**
     * @var ProductsRepository
     */
    protected $skus_repo;

    public function __construct(ProductSkusRepository $skus_repo)
    {
        $this->skus_repo = $skus_repo;
    }

    public function index(Request $request){
        $type = $request->get('type', 'page');
        if($type == 'page'){
            $skus = $this->skus_repo->with(['product'])->paginate($this->perPage);
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, $skus, true);
        }elseif($type == 'all'){
            $skus = $this->skus_repo->with(['product'])->all();
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, $skus['data']);
        }
    }

    public function show($sku_id){
        $product_info = $this->skus_repo->with(['product'])->find($sku_id);
        
        return $this->success(ErrorConst::SUCCESS_CODE, $product_info['data']);
    }

    /**
     * 商品评论列表
     * @param OrderProductCommentRepository $order_product_comment
     * @param $product_id
     * @return \Illuminate\Support\Collection
     */
    public function indexProductsComments(OrderProductCommentRepository $order_product_comment, $product_id){
        $comments = $order_product_comment->scopeQuery(function($query) use ($product_id){
            return $query->where('product_id', $product_id);
        })->paginate($this->perPage);

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $comments);
    }
}
